﻿#region Using Directives

using System;
using System.Web.Caching;
using System.Web.Security;
using System.Collections.Generic;

using QuickDev.Common.Serialization;
using QuickDev.Common.Caching.Manager;

using QuickDev.Model;
using QuickDev.SQLServerDAL;
using QuickDev.Common.Utils;

#endregion

namespace QuickDev.Caching
{
    /// <summary>
    /// Cache Layer For dbo.SYS_ServiceConfiguration.
    /// </summary>
    public class ServiceConfigurationCache
    {
        #region Constants

        private const string CONFIGURATION_SERVICE = "Configuration Service Name";
        private const string ServiceConfigurationModelCacheKey = "_ServiceConfiguration_CacheKey_{0}";
        private const string FetchServiceConfigurationsByServiceCacheKey = "_ServiceConfiguration_Collection_FetchServiceConfigurationsByService_{0}_{1}";

        #endregion

        #region Refresh Cache

        /// <summary>
        /// Refreshes the collection in the cache.
        /// This will make that the collection should reinsert itself on the next page request
        /// </summary>
        public static void RemoveAllCollectionFromCache()
        {
            CacheHelper.RemoveAllCacheObjectStartWith( FetchServiceConfigurationsByServiceCacheKey.Substring( 0, FetchServiceConfigurationsByServiceCacheKey.LastIndexOf( "FetchServiceConfigurationsByService" ) + "FetchServiceConfigurationsByService".Length ) );
        }

        /// <summary>
        /// Removes the Object Info from the cache.
        /// This will make that the Object should reinsert itself on the next page request
        /// </summary>
        /// <param name="key">The key.</param>
        public static void RemoveModelFromCache( int configurationId )
        {
            CacheHelper.RemoveCacheObject<string>( string.Format( ServiceConfigurationModelCacheKey, configurationId ) );
            CacheHelper.RemoveCacheObject<string>( string.Format( ServiceConfigurationModelCacheKey, GetModel( configurationId ).Service ) );
        }

        #endregion

        #region ServiceConfigurationCache.GetModel( int configurationId ) Method

        /// <summary>
        /// public ServiceConfiguration GetModel( int configurationId )
        /// </summary>
        /// <param name="configurationId">configurationId</param>
        /// <returns>ServiceConfiguration model</returns>
        public static ServiceConfiguration GetModel( int configurationId )
        {
            Validation.ValidateIntegerIsGreaterThanZero( configurationId, "QuickDev.Caching.ServiceConfigurationCache.GetModel -- configurationId " );

            return CacheHelper.CacheObject<ServiceConfiguration>
            (
                delegate
                {
                    return ServiceConfigurationDAL.GetModel( configurationId );
                },
                string.Format( ServiceConfigurationModelCacheKey, configurationId ),
                CacheLength.GetLongCacheTime,
                CacheItemPriority.High
            );
        }
        #endregion

        #region ServiceConfigurationCache.GetModel( string service ) Method

        /// <summary>
        /// public ServiceConfiguration GetModel( string service )
        /// </summary>
        /// <param name="service">service</param>
        /// <returns>ServiceConfiguration model</returns>
        public static ServiceConfiguration GetModel( string service )
        {
            Validation.ValidateStringArgumentIsNotNullOrEmptyString( service, "QuickDev.Caching.ServiceConfigurationCache.GetModel -- service " );

            return CacheHelper.CacheObject<ServiceConfiguration>
            (
                delegate
                {
                    return ServiceConfigurationDAL.GetModel( service );
                },
                string.Format( ServiceConfigurationModelCacheKey, service ),
                CacheLength.GetLongCacheTime,
                CacheItemPriority.High
            );
        }
        #endregion

        #region ServiceConfigurationCache.FetchConfigurationsByService( CollectionsStatus status , string service ) Method

        /// <summary>
        /// public IList(ServiceConfiguration) FetchConfigurationsByService( CollectionsStatus status , string service )
        /// </summary>
        /// <param name="status">status</param>
        /// <param name="service">service</param>
        /// <returns>IList(ServiceConfiguration)</returns>
        public static List<ServiceConfiguration> FetchConfigurationsByService( CollectionsStatus status, string service )
        {
            Validation.ValidateStringArgumentIsNotNullOrEmptyString( service, "QuickDev.Caching.ServiceConfigurationCache.FetchConfigurationsByService() Method -- service" );

            return CacheHelper.CacheObject<List<ServiceConfiguration>>
            (
                delegate
                {
                    return ServiceConfigurationDAL.FetchConfigurationsByService( service, (int)status );
                },
                string.Format( FetchServiceConfigurationsByServiceCacheKey, status, service ),
                CacheLength.GetLongCacheTime,
                CacheItemPriority.High
            );
        }
        #endregion

        #region ServiceConfigurationDAL.FetchConfigurationByService( string service ) Method

        /// <summary>
        /// Fetches the configuration by service.
        /// </summary>
        /// <param name="service">Name of the configuration service.</param>
        /// <returns></returns>
        public static object FetchConfigurationByService( string service )
        {
            Validation.ValidateStringArgumentIsNotNullOrEmptyString( service, CONFIGURATION_SERVICE );

            ServiceConfiguration serviceConfiguration = ServiceConfigurationCache.GetModel( service );

            object obj = null;

            if ( serviceConfiguration.ConfigurationId > 0 )
            {
                Serializer serializer = new Serializer();
                obj = serializer.DeserializeObject( serviceConfiguration.Value, serviceConfiguration.Type );
            }
            return obj;
        }

        #endregion
    }
}

